#!/usr/bin/python
import os,sys,re

# Modify Linux 2.6.28.8 kernel `.config' file as follows:
# < # CONFIG_SYSFS_DEPRECATED_V2 is not set
# > CONFIG_SYSFS_DEPRECATED=y
# > CONFIG_SYSFS_DEPRECATED_V2=y
# < CONFIG_AEABI=y
# < # CONFIG_OABI_COMPAT is not set
# > # CONFIG_AEABI is not set
# > CONFIG_FPE_NWFPE=y
# > # CONFIG_FPE_NWFPE_XP is not set
# > # CONFIG_FPE_FASTFPE is not set
# > # CONFIG_ARTHUR is not set
# < # CONFIG_MTD_NAND_ATMEL_ECC_HW is not set
# < CONFIG_MTD_NAND_ATMEL_ECC_SOFT=y
# > CONFIG_MTD_NAND_ATMEL_ECC_HW=y
# > # CONFIG_MTD_NAND_ATMEL_ECC_SOFT is not set
# > CONFIG_USB_HID=y
# < # CONFIG_USB_HID is not set
# > # CONFIG_USB_HIDDEV is not set
# < #
# < # USB HID Boot Protocol drivers
# < #
# < # CONFIG_USB_KBD is not set
# < # CONFIG_USB_MOUSE is not set
# > CONFIG_HID_A4TECH=y
# > CONFIG_HID_APPLE=y
# > CONFIG_HID_BELKIN=y
# > CONFIG_HID_BRIGHT=y
# > CONFIG_HID_CHERRY=y
# > CONFIG_HID_CHICONY=y
# > CONFIG_HID_CYPRESS=y
# > CONFIG_HID_DELL=y
# > CONFIG_HID_EZKEY=y
# > CONFIG_HID_GYRATION=y
# > CONFIG_HID_LOGITECH=y
# > # CONFIG_LOGITECH_FF is not set
# > # CONFIG_LOGIRUMBLEPAD2_FF is not set
# > CONFIG_HID_MICROSOFT=y
# > CONFIG_HID_MONTEREY=y
# > CONFIG_HID_PANTHERLORD=y
# > # CONFIG_PANTHERLORD_FF is not set
# > CONFIG_HID_PETALYNX=y
# > CONFIG_HID_SAMSUNG=y
# > CONFIG_HID_SONY=y
# > CONFIG_HID_SUNPLUS=y
# > # CONFIG_THRUSTMASTER_FF is not set
# > # CONFIG_ZEROPLUS_FF is not set

indices = xrange(sys.maxint)
f=open('.config','r')
contents=f.readlines()
f.close()

def match(s):
	found=-1
	for c, i in zip(contents, indices):
		if re.search(s, c):
			if found!=-1:
				raise Exception('more than two lines match.', s)
			found=i
	if found==-1: raise Exception('No line matches.', s)
	return found

replaces={
'CONFIG_SYSFS_DEPRECATED=y':'',
'CONFIG_SYSFS_DEPRECATED_V2=y':'# CONFIG_SYSFS_DEPRECATED_V2 is not set',
'# CONFIG_AEABI is not set':'CONFIG_AEABI=y\n# CONFIG_OABI_COMPAT is not set',
'CONFIG_FPE_NWFPE=y':'',
'# CONFIG_FPE_NWFPE_XP is not set':'',
'# CONFIG_FPE_FASTFPE is not set':'',
'# CONFIG_ARTHUR is not set':'',
'CONFIG_MTD_NAND_ATMEL_ECC_HW=y':'# CONFIG_MTD_NAND_ATMEL_ECC_HW is not set',
'# CONFIG_MTD_NAND_ATMEL_ECC_SOFT is not set':'CONFIG_MTD_NAND_ATMEL_ECC_SOFT=y',
'# CONFIG_USB_HID is not set':'CONFIG_USB_HID=y\n# CONFIG_USB_HIDDEV is not set',
'# CONFIG_USB_KBD is not set':'',
'# CONFIG_USB_MOUSE is not set':'''CONFIG_HID_A4TECH=y
CONFIG_HID_APPLE=y
CONFIG_HID_BELKIN=y
CONFIG_HID_BRIGHT=y
CONFIG_HID_CHERRY=y
CONFIG_HID_CHICONY=y
CONFIG_HID_CYPRESS=y
CONFIG_HID_DELL=y
CONFIG_HID_EZKEY=y
CONFIG_HID_GYRATION=y
CONFIG_HID_LOGITECH=y
# CONFIG_LOGITECH_FF is not set
# CONFIG_LOGIRUMBLEPAD2_FF is not set
CONFIG_HID_MICROSOFT=y
CONFIG_HID_MONTEREY=y
CONFIG_HID_PANTHERLORD=y
# CONFIG_PANTHERLORD_FF is not set
CONFIG_HID_PETALYNX=y
CONFIG_HID_SAMSUNG=y
CONFIG_HID_SONY=y
CONFIG_HID_SUNPLUS=y
# CONFIG_THRUSTMASTER_FF is not set
# CONFIG_ZEROPLUS_FF is not set''',
}

for k in replaces.keys(): match(k) # check one match and only one match
for k in replaces.keys(): contents[match(k)]=replaces[k]+'\n'

f=open('.config','w')
for c in contents: f.write(c)
f.close()
